/*
 * Achieve it All! - Android app to motivate achieving your goals
 * Copyright (C) 2014  Xilconic
 * 
 * This file is part of Achieve it All!.
 *
 * Achieve it All! is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Achieve it All! is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Achieve it All!.  If not, see <http://www.gnu.org/licenses/>
 */
package com.xilconic.achieveitall;

import com.xilconic.achieveitall.domain.Goal;
import com.xilconic.achieveitall.domain.GoalsOverview;
import com.xilconic.achieveitall.domain.Target;
import com.xilconic.achieveitall.fragments.UserProfileFragment;
import com.xilconic.achieveitall.fragments.goalsoverview.GoalsOverviewFragment;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class GoalTargetsActivity extends ActionBarActivity implements
        ActionBar.OnNavigationListener {
	private GoalsOverview overview;

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current dropdown position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goal_targets);

        // Set up the action bar to show a dropdown list.
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        // Set up the dropdown list navigation in the action bar.
        actionBar.setListNavigationCallbacks(
        // Specify a SpinnerAdapter to populate the dropdown list.
                new ArrayAdapter<String>(actionBar.getThemedContext(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1, new String[] {
                                getString(R.string.title_section1),
                                getString(R.string.title_section2),
                                getString(R.string.title_section3), }), this);
        
        overview = new GoalsOverview();
        setDummyGoalsOverviewData();
    }

    private void setDummyGoalsOverviewData() {
    	// Good foundations goal:
    	Goal goal = new Goal();
    	goal.setName("Good foundations");
    	
    	Target target = new Target();
    	target.setName("Be an early bird.");
    	goal.addTarget(target);
    	
    	target = new Target();
    	target.setName("Brush you teeth 2/day.");
    	goal.addTarget(target);
    	
    	target = new Target();
    	target.setName("Go to bed on time.");
    	goal.addTarget(target);
    	
		overview.addGoal(goal);
		
		// Good Beginnings goal:
		goal = new Goal();
    	goal.setName("Good beginnings");
    	
    	target = new Target();
    	target.setName("Have breakfast.");
    	goal.addTarget(target);
    	
    	target = new Target();
    	target.setName("Walk 3/week.");
    	goal.addTarget(target);
    	
    	target = new Target();
    	target.setName("Eat a piece of fruit.");
    	goal.addTarget(target);
    	
		overview.addGoal(goal);
		
		// Accept all goals:
		overview.acceptGoal(0);
		overview.acceptGoal(0);
	}

	@Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current dropdown position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getSupportActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current dropdown position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getSupportActionBar()
                .getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.goal_targets, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(int position, long id) {
        // When the given dropdown item is selected, show its contents in the
        // container view.
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, createFragmentForMenuSection(position)).commit();
        return true;
    }

	private Fragment createFragmentForMenuSection(int position) {
		if(position == 0){
			return UserProfileFragment.newInstance();
		}
		if(position == 1){
			return GoalsOverviewFragment.newInstance(overview);
		}
		return PlaceholderFragment.newInstance(position + 1);
	}

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_goal_targets,
                    container, false);
            TextView textView = (TextView) rootView
                    .findViewById(R.id.section_label);
            textView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

}
